package com.eye.catcher.service.generate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eye.catcher.common.RedisUtils;
import com.eye.catcher.constants.RedisConstants;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TYwDaBaseInfo;
import com.eye.catcher.entity.TYwDaEquipmentInfo;
import com.eye.catcher.entity.TYwDaFieldInfo;
import com.eye.catcher.entity.TYwDaTemperatureHumidityInfo;
import com.eye.catcher.mapper.TYwDaTemperatureHumidityInfoMapper;
import com.eye.catcher.service.generate.ITYwDaEquipmentInfoService;
import com.eye.catcher.service.generate.ITYwDaFieldInfoService;
import com.eye.catcher.service.generate.ITYwDaTemperatureHumidityInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eye.catcher.system.role.param.RoleDto;
import com.eye.catcher.system.user.param.UserLoginInfo;
import com.eye.catcher.system.yw.Info.param.TemperatureHumidityInfoDto;
import com.eye.catcher.system.yw.Info.param.TemperatureHumidityInfoPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 温湿度 表 服务实现类
 * </p>
 *
 * @author huzhiwei
 * @since 2022-05-12
 */
@Slf4j
@Service
public class TYwDaTemperatureHumidityInfoServiceImpl extends ServiceImpl<TYwDaTemperatureHumidityInfoMapper, TYwDaTemperatureHumidityInfo> implements ITYwDaTemperatureHumidityInfoService {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ITYwDaFieldInfoService fieldInfoService;
    @Autowired
    private ITYwDaEquipmentInfoService equipmentInfoService;
    @Autowired
    private TYwDaTemperatureHumidityInfoMapper temperatureHumidityInfoMapper;

    @Value("${base.all-permissions.roleId}")
    private String baseAllPermissionsRoleId;
    @Override
    public void addTemperatureHumidityInfo(TYwDaTemperatureHumidityInfo info) {
        UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(RedisConstants.USER_INFO_EXPIRE.getKey()+info.getCreateUserId());
        info.setCreateUserId(info.getCreateUserId());
        info.setCreateUserName(userLoginInfo.getUserDto().getUserName());
        info.setCreateLoginName(userLoginInfo.getUserDto().getLoginName());
        info.setCreateTime(LocalDateTime.now());
        info.setSource(SbCode.DATA_SOURCE_CREATE.getCode());
        if(null != info.getFieldId()){
            TYwDaFieldInfo fieldInfo = fieldInfoService.getById(info.getFieldId());
            if(null != fieldInfo) {
                info.setFieldCode(fieldInfo.getFieldCode());
                info.setFieldName(fieldInfo.getFieldName());
            }
        }
        if(null != info.getEquipmentId()){
            TYwDaEquipmentInfo equipmentInfo = equipmentInfoService.getById(info.getEquipmentId());
            if(null != equipmentInfo) {
                info.setEquipmentCode(equipmentInfo.getEquipmentCode());
                info.setEquipmentName(equipmentInfo.getEquipmentName());
            }
        }
        this.save(info);

    }

    @Override
    public void editTemperatureHumidityInfo(TYwDaTemperatureHumidityInfo info) {
        UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(RedisConstants.USER_INFO_EXPIRE.getKey()+info.getUpdateUserId());
        info.setUpdateUserId(info.getCreateUserId());
        info.setUpdateUserName(userLoginInfo.getUserDto().getUserName());
        info.setUpdateLoginName(userLoginInfo.getUserDto().getLoginName());
        info.setUpdateTime(LocalDateTime.now());
        if(null != info.getFieldId()){
            TYwDaFieldInfo fieldInfo = fieldInfoService.getById(info.getFieldId());
            if(null != fieldInfo) {
                info.setFieldCode(fieldInfo.getFieldCode());
                info.setFieldName(fieldInfo.getFieldName());
            }
        }
        if(null != info.getEquipmentId()){
            TYwDaEquipmentInfo equipmentInfo = equipmentInfoService.getById(info.getEquipmentId());
            if(null != equipmentInfo) {
                info.setEquipmentCode(equipmentInfo.getEquipmentCode());
                info.setEquipmentName(equipmentInfo.getEquipmentName());
            }
        }
        this.updateById(info);
    }

    @Override
    public void deleteByIds(String ids) {
        this.removeByIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public Object getAllList(TemperatureHumidityInfoDto temperatureHumidityInfo) {
        QueryWrapper<TYwDaTemperatureHumidityInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,temperatureHumidityInfo);

        wrapper.orderByAsc(" SORT_NO ");
        return temperatureHumidityInfoMapper.list(wrapper);
    }

    @Override
    public Object getPageList(TemperatureHumidityInfoPage temperatureHumidityInfoPage) {
        Page<TYwDaTemperatureHumidityInfo> page=new Page<>(temperatureHumidityInfoPage.getPage().getPageNum(),temperatureHumidityInfoPage.getPage().getPageSize());
        QueryWrapper<TYwDaTemperatureHumidityInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,temperatureHumidityInfoPage.getTemperatureHumidityInfo());

        wrapper.orderByAsc(" SORT_NO ");
        //list
        IPage<TYwDaTemperatureHumidityInfo> returnPage = temperatureHumidityInfoMapper.page(page, wrapper);
        //总数
        returnPage.setTotal(temperatureHumidityInfoMapper.count(wrapper));
        return returnPage;
    }

    private void appendWrapper(QueryWrapper<TYwDaTemperatureHumidityInfo> wrapper, TemperatureHumidityInfoDto temperatureHumidityInfo) {

        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getId())){
            wrapper.eq("ID",Integer.parseInt(temperatureHumidityInfo.getId()));
        }
        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getFieldId())){
            wrapper.eq("FIELD_ID",Integer.parseInt(temperatureHumidityInfo.getFieldId()));
        }
        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getFieldCode())){
            wrapper.eq("FIELD_CODE",temperatureHumidityInfo.getFieldCode());
        }
        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getFieldName())){
            wrapper.like("FIELD_NAME",temperatureHumidityInfo.getFieldName());
        }
        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getEquipmentId())){
            wrapper.eq("EQUIPMENT_ID",Integer.parseInt(temperatureHumidityInfo.getEquipmentId()));
        }
        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getEquipmentCode())){
            wrapper.eq("EQUIPMENT_CODE",temperatureHumidityInfo.getEquipmentCode());
        }
        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getEquipmentName())){
            wrapper.like("EQUIPMENT_NAME",temperatureHumidityInfo.getEquipmentName());
        }
        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getCreateUserId())){
            wrapper.eq("CREATE_USER_ID",temperatureHumidityInfo.getCreateUserId());
        }

        if(StringUtils.isNotEmpty(temperatureHumidityInfo.getUserId())){
            UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(RedisConstants.USER_INFO_EXPIRE.getKey()+temperatureHumidityInfo.getUserId());
            if(!"admin".equals(userLoginInfo.getUserDto().getLoginName())){
                Set<String> rolesSet =new HashSet<>(
                        userLoginInfo.getRoleDtoList().stream().map(RoleDto::getId).collect(Collectors.toList())
                );
                if(!rolesSet.contains(baseAllPermissionsRoleId)){
                    wrapper.eq("q.baseId",userLoginInfo.getUserDto().getBaseId());
                }
            }
        }
    }
}
